package com.zx.servicelearn.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.servicelearn.R;
import com.zx.servicelearn.aidl.IBookManager;
import com.zx.servicelearn.aidl.IOnNewBookArrivedListener;
import com.zx.servicelearn.model.Book;
import com.zx.servicelearn.myaidl.IBookManager2;
import com.zx.servicelearn.service.IPCService1_Messenger;
import com.zx.servicelearn.service.IPCService2_AIDL;
import com.zx.servicelearn.service.IPCService3_MYAIDL;
import com.zx.servicelearn.utils.MyUtils;

import java.util.List;

/**
 * 模拟客户端进程
 */
public class IPCLearnActivity3 extends AppCompatActivity {
    TextView textshow_tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ipclearn3);
        textshow_tv = findViewById(R.id.textshow_tv);
        Intent intent = new Intent(this, IPCService3_MYAIDL.class);
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    IBookManager2 mIBookManager2;
    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIBookManager2 = IBookManager2.BookManager2Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void test1(View view) {
        try {
            if (mIBookManager2 == null) {//如果服务端在onBind中做了权限验证，可能会导致绑定失败,mIBookManager为空
                Toast.makeText(this, "绑定服务端失败！ mIBookManager is null", Toast.LENGTH_SHORT).show();
                return;
            }
            mIBookManager2.add(new Book(1, "一本好书"));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(mServiceConnection);
        super.onDestroy();

    }

    public void test2(View view) {
        try {
            if (mIBookManager2 == null) {//如果服务端在onBind中做了权限验证，可能会导致绑定失败,mIBookManager为空
                Toast.makeText(this, "绑定服务端失败！ mIBookManager is null", Toast.LENGTH_SHORT).show();
                return;
            }
            List<Book> mList = mIBookManager2.getList();
            String test = "";
            for (int i = 0; i < mList.size(); i++) {
                Log.e("IPCLearnActivity2", "书" + i + "：msg——————" + mList.get(i).toString());
                test += "\n书" + i + "：msg——————" + mList.get(i).toString();

            }
            textshow_tv.setText(test);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
